package poi.controller;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import poi.entity.Appraisal;
import poi.service.PoiService;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 程昱轩
 * @Description: TODO
 * @DateTime: @date 2023/3/15 10:26
 **/
@RestController
@RequestMapping("/poi")
public class PoiController {
    @Resource
    private PoiService poiService;
    @RequestMapping("/export")
    public void export() throws IOException {
        List<Appraisal> list=poiService.findAll();
        System.out.println(list+"--------------");
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet xssfSheet = xssfWorkbook.createSheet("问卷测评表");
        XSSFRow row0 = xssfSheet.createRow(0);
        row0.createCell(0).setCellValue("测评IDaaa");
        row0.createCell(1).setCellValue("测评内容");
        row0.createCell(2).setCellValue("测评选项");
        int i=1;
        for (Appraisal appraisal : list) {
            XSSFRow row = xssfSheet.createRow(i);
            row.createCell(0).setCellValue(appraisal.getAppraisalId());
            row.createCell(1).setCellValue(appraisal.getAppraisalName());
            row.createCell(2).setCellValue(appraisal.getOptionName());
            i++;
        }
        String excelPath = "C:\\Users\\Cheng_yuxuan\\Desktop\\test\\test.xlsx";
        FileOutputStream fileOutputStream = new FileOutputStream(excelPath);
        xssfWorkbook.write(fileOutputStream);
        fileOutputStream.close();
    }
    @RequestMapping("imports")
    public void imports() throws IOException{
//        获取excel文件
        String excelPath = "C:\\Users\\Cheng_yuxuan\\Desktop\\test\\test.xlsx";
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(excelPath));
//        获取其中的一个sheet表
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);

        int startNum = xssfSheet.getFirstRowNum() + 1;
        int endNum = xssfSheet.getLastRowNum();

        for (int i = startNum; i <= endNum; i++){
//            获取行
            XSSFRow xssfRow = xssfSheet.getRow(i);
//            获取列开始
            int cellStartNum = xssfRow.getFirstCellNum();
//            获取列结束
            int cellEndNum = xssfRow.getLastCellNum();
            for (int j = cellStartNum; j < cellEndNum; j++){
                System.out.print(xssfRow.getCell(j).getStringCellValue());
            }
            System.out.println();
        }


    }


}
